-
Notifications
You must be signed in to change notification settings - Fork 98
Fix SFTP server hang on WS_WANT_WRITE with non-blocking sockets #876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
When wolfSSH_SFTP_buffer_send() called wolfSSH_stream_send(), the data would be consumed into the SSH output buffer even if the underlying socket returned EWOULDBLOCK/EAGAIN. SendChannelData() returns the positive dataSz on WS_WANT_WRITE, causing the SFTP layer to advance its buffer index as if the data was sent. The SSH output buffer still had pending data that was never flushed, leading to an indefinite hang. Fix: At the start of wolfSSH_SFTP_buffer_send(), check if there's pending data in ssh->outputBuffer from a previous WS_WANT_WRITE. If so, attempt to flush it first and return WS_WANT_WRITE if the flush fails. This ensures the caller retries until all pending data is sent. Also expose WS_SFTP_BUFFER and wolfSSH_SFTP_buffer_send() as WOLFSSH_LOCAL for unit testing, and add regression test that verifies the fix catches the bug. Fixes ZD 21157
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Fixes an SFTP server hang in non-blocking mode when the underlying socket returns EWOULDBLOCK/EAGAIN (mapped to WS_WANT_WRITE) by ensuring previously-buffered SSH output is flushed before consuming more SFTP-layer data, and by preventing the SFTP receive state machine from treating buffered-but-unsent data as fully transmitted.
Changes:
- Add a flush-first step in
wolfSSH_SFTP_buffer_send()whenssh->outputBufferalready has pending bytes. - Update
wolfSSH_SFTP_read()to treat “SFTP buffer fully consumed but SSH output still pending” asWS_WANT_WRITE. - Expose
WS_SFTP_BUFFERandwolfSSH_SFTP_buffer_send()for unit/regression testing and add CI coverage to detect hangs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
wolfssh/wolfsftp.h |
Moves WS_SFTP_BUFFER to the header and exposes wolfSSH_SFTP_buffer_send() as WOLFSSH_LOCAL to enable unit testing. |
src/wolfsftp.c |
Flushes pending SSH output before sending new SFTP buffer data; adds a WS_WANT_WRITE guard when SSH still has unsent buffered output. |
tests/regress.c |
Adds a regression test that simulates pending SSH output + WS_WANT_WRITE and verifies the SFTP buffer is not consumed. |
.github/workflows/paramiko-sftp-test.yml |
Enhances Paramiko SFTP workflow with a repeated-GET stress test and hang detection timeout. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When wolfSSH_SFTP_buffer_send() called wolfSSH_stream_send(), the data would be consumed into the SSH output buffer even if the underlying socket returned EWOULDBLOCK/EAGAIN. SendChannelData() returns the positive dataSz on WS_WANT_WRITE, causing the SFTP layer to advance its buffer index as if the data was sent. The SSH output buffer still had pending data that was never flushed, leading to an indefinite hang.
Fix: At the start of wolfSSH_SFTP_buffer_send(), check if there's pending data in ssh->outputBuffer from a previous WS_WANT_WRITE. If so, attempt to flush it first and return WS_WANT_WRITE if the flush fails. This ensures the caller retries until all pending data is sent.
Also expose WS_SFTP_BUFFER and wolfSSH_SFTP_buffer_send() as WOLFSSH_LOCAL for unit testing, and add regression test that verifies the fix catches the bug.
Fixes ZD 21157